package com.sks.utils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class WebAppContextUtil {
	
	private static WebApplicationContext context;

	public static void init(ServletContext sc) {
		context = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
	}
	
	public static WebApplicationContext getWebAppContext() {
		return context;
	}

	public static Object getBean(String name) throws ServletException {
		if (null == context) {
			throw new ServletException("has not init context");
		}
		return context.getBean(name);
	}
}
